!
! SYS_SIZE is the number of clicks (16 bytes) to be loaded.
! 0x3000 is 0x30000 bytes = 196kB, more than enough for current
! versions of linux
!
SYSSIZE = 0x3000 ! linux 0.11 bootsec需要加载的system模块的字节数目, 实际上是0x30000（192KB），这与保护模式下的地址转换相关
!
!	bootsect.s		(C) 1991 Linus Torvalds
!
! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves
! iself out of the way to address 0x90000, and jumps there.
!
! It then loads 'setup' directly after itself (0x90200), and the system
! at 0x10000, using BIOS interrupts. 
!
! NOTE! currently system is at most 8*65536 bytes long. This should be no
! problem, even in the future. I want to keep it simple. This 512 kB
! kernel size should be enough, especially as this doesn't contain the
! buffer cache as in minix
!
! The loader has been made as simple as possible, and continuos
! read errors will result in a unbreakable loop. Reboot by hand. It
! loads pretty fast by getting whole sectors at a time whenever possible.

.globl begtext, begdata, begbss, endtext, enddata, endbss
.text
begtext:
.data
begdata:
.bss
begbss:
.text

SETUPLEN = 4				! nr of setup-sectors ! setup程序的扇区数值
BOOTSEG  = 0x07c0			! original address of boot-sector ! 实际地址是0x7c00,但是写成0x07c0与保护模式下的地址转换相关
INITSEG  = 0x9000			! we move boot here - out of the way
SETUPSEG = 0x9020			! setup starts here ! setup的起始地址
SYSSEG   = 0x1000			! system loaded at 0x10000 (65536). ! system模块的起始地址
ENDSEG   = SYSSEG + SYSSIZE		! where to stop loading !停止加载的段地址

! ROOT_DEV:	0x000 - same type of floppy as boot. !根文件系统设备使用软驱设备
!		0x301 - first partition on first drive etc !在第一个硬盘的第一个分区上
ROOT_DEV = 0x306 ! 指定根文件系统设备是第二个硬盘的第一个分区

entry _start
_start: !进入bootsect时，将自身代码移动到0x90000处(47行至56行)
	mov	ax,#BOOTSEG 
	mov	ds,ax
	mov	ax,#INITSEG
	mov	es,ax
	mov	cx,#256 ! 总计移动256个word
	sub	si,si !源地址ds:si
	sub	di,di !目的地址es:di
	rep
	movw !移动一个字
	jmpi	go,INITSEG !段间跳转。这里INITSEG指出跳转到的段地址,INITSEG是段基地址。含义是跳转到段基址为INITSEG，label为go的汇编代码处开始执行
go:	mov	ax,cs ! 从这里开始，所有的代码执行都是在0x9000 的段基址下进行
	mov	ds,ax ! 把ds和es设置成与cs相同，即：0x9000
	mov	es,ax
! put stack at 0x9ff00.
	mov	ss,ax ! 栈基址此时为 ss:sp
	mov	sp,#0xFF00		! arbitrary value >>512 ! 这里的栈基址的设置必须要越过bootsect和setup的代码段

! load the setup-sectors directly after the bootblock.
! Note that 'es' is already set up.

load_setup:
	mov	dx,#0x0000		! drive 0, head 0
	mov	cx,#0x0002		! sector 2, track 0 ! 从2号扇区开始加载数据
	mov	bx,#0x0200		! address = 512, in INITSEG
	mov	ax,#0x0200+SETUPLEN	! service 2, nr of sectors
	int	0x13			! read it
	jnc	ok_load_setup		! ok - continue
	mov	dx,#0x0000
	mov	ax,#0x0000		! reset the diskette
	int	0x13
	j	load_setup

ok_load_setup:

! Get disk drive parameters, specifically nr of sectors/track. ! 取磁盘驱动器参数，特别是每道扇区数量。

	mov	dl,#0x00
	mov	ax,#0x0800		! AH=8 is get drive parameters
	int	0x13
	mov	ch,#0x00
	seg cs ! 表示下一条语句的操作数在cs段寄存器所指的段中。
	mov	sectors,cx !保存每磁道扇区数到变量sectors中
	mov	ax,#INITSEG
	mov	es,ax ! 因为取磁盘驱动器参数时修改了es，这里进行还原

! Print some inane message ! 这里显示的是 "Loading system..."

	mov	ah,#0x03		! read cursor pos
	xor	bh,bh
	int	0x10
	
	mov	cx,#24
	mov	bx,#0x0007		! page 0, attribute 7 (normal)
	mov	bp,#msg1
	mov	ax,#0x1301		! write string, move cursor
	int	0x10

! ok, we've written the message, now
! we want to load the system (at 0x10000)

	mov	ax,#SYSSEG ! 设置system段基址
	mov	es,ax		! segment of 0x010000
	call	read_it
	call	kill_motor !关闭驱动器电机

! After that we check which root-device to use. If the device is
! defined (!= 0), nothing is done and the given device is used.
! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
! on the number of sectors that the BIOS reports currently.

	seg cs
	mov	ax,root_dev
	cmp	ax,#0
	jne	root_defined
	seg cs
	mov	bx,sectors
	mov	ax,#0x0208		! /dev/ps0 - 1.2Mb
	cmp	bx,#15
	je	root_defined
	mov	ax,#0x021c		! /dev/PS0 - 1.44Mb
	cmp	bx,#18
	je	root_defined
undef_root:
	jmp undef_root
root_defined:
	seg cs
	mov	root_dev,ax

! after that (everyting loaded), we jump to
! the setup-routine loaded directly after
! the bootblock:

	jmpi	0,SETUPSEG

! This routine loads the system at address 0x10000, making sure
! no 64kB boundaries are crossed. We try to load it as fast as
! possible, loading whole tracks whenever we can.
!
! in:	es - starting address segment (normally 0x1000)
!
sread:	.word 1+SETUPLEN	! sectors read of current track
head:	.word 0			! current head
track:	.word 0			! current track

read_it:
	mov ax,es ! 加载的时候是将setup加载到了额外段, 此时 es == 0x1000
	test ax,#0x0fff ! 测试输入的段值。从盘上读入的数据必须存放在内存地址64KB(0x10000)的边界开始处，否则将进入死循环。
die:	jne die			! es must be at 64kB boundary ! jne 当CF=0时转移至die CF记录了test的结果
	xor bx,bx		! bx is starting address within segment !清除bx寄存器，bx是段内偏移位置
rp_read: 
	mov ax,es
	cmp ax,#ENDSEG		! have we loaded all yet? ! 判断是否已经读入全部数据
	jb ok1_read ! 如果当前所读段不是系统数据末端所处的段，就跳转到ok1_read处继续读数据，否则返回
	ret
ok1_read:!计算和验证当前磁道需要读取的扇区数，放在ax寄存器中。根据当前磁道还未读取的扇区数以及段内数据字节开始偏移位置，计算如果全部读取这些未读扇区，所读总字节数是否会超过
	seg cs
	mov ax,sectors ! 这里的sectors变量保存的是每磁道扇区数，在第88行写入
	sub ax,sread ! 减去当前磁道已读扇区数
	mov cx,ax
	shl cx,#9 ! cx = cx * 512 字节， 因为一个扇区有512字节
	add cx,bx ! cx = cx + 段内偏移值
	jnc ok2_read ! 若没有超过64KB(0x10000)，则跳转至ok2_read处执行。JNC也可以看成是加法没有进位，减法没有借位的时候转移。ax是16bit，当有进位的时候表示 和 >= 0x10000
	je ok2_read ! 功能是在ZF等于0时进行跳转，也就是相等的时候跳转。此时cx == 0x10000.因为上一步如果没有跳转代表有进位，此时ax == 0，则说明 和 == 0x10000。
	xor ax,ax!此时和大于0x10000。
	sub ax,bx!计算此时能读入的字节数(64KB-端内偏移位置)
	shr ax,#9!将字节数转换成扇区数
ok2_read:!读当前磁道上指定开始扇区和需读取扇区数的数据到es:bx开始处。al: 需读扇区数；es:bx: 缓冲区开始位置
	call read_track
	mov cx,ax ! cx = 此次操作已读取的扇区数
	add ax,sread ! 当前磁道上已经读取的扇区数
	seg cs
	cmp ax,sectors  ! 如果当前磁道上还有扇区未读，则跳转至ok3_read
	jne ok3_read 
	mov ax,#1 ! 读取磁道另一磁头上的数据。如果已经完成，则去读下一磁道
	sub ax,head ! 判断当前磁头号。因为磁头号不是0就是1,1-0=1,1-1=0
	jne ok4_read ! 如果是0磁头则再去读1磁头面上的扇区数据
	inc track ! 否则去读下一磁道
ok4_read:
	mov head,ax ! 保存当前磁头号
	xor ax,ax ! 清当前磁道已读扇区数
ok3_read:
	mov sread,ax ! 保存当前已读扇区数
	shl cx,#9 !上次已读扇区*512B
	add bx,cx! 调整当前段内数据开始位置
	jnc rp_read ! 若小于64KB边界则跳转到rp_read,否则调整当前段为读下一段数据做准备
	mov ax,es
	add ax,#0x1000!调整段基址，指向下一段
	mov es,ax
	xor bx,bx
	jmp rp_read

read_track:!读当前次当上指定开始扇区和需读扇区数的数据到es:bx开始处。al: 需读扇区数；es:bx: 缓冲区开始位置
	push ax
	push bx
	push cx
	push dx
	mov dx,track ! 取当前磁道号
	mov cx,sread ! 取当前磁道上已读扇区数
	inc cx ! cl = 开始读扇区
	mov ch,dl ! ch = 当前磁道号
	mov dx,head ! 取当前磁头号
	mov dh,dl ! dh = 磁头号
	mov dl,#0 ! dl = 驱动器号 0表示当前驱动器
	and dx,#0x0100 ! 磁头号不大于1
	mov ah,#2 ! ah = 2, 读磁盘扇区功能号
	int 0x13 
	jc bad_rt ! 若出错转至bad_rt
	pop dx
	pop cx
	pop bx
	pop ax
	ret
bad_rt:	mov ax,#0
	mov dx,#0
	int 0x13
	pop dx
	pop cx
	pop bx
	pop ax
	jmp read_track

!/*
! * This procedure turns off the floppy drive motor, so
! * that we enter the kernel in a known state, and
! * don't have to worry about it later.
! */
kill_motor:
	push dx
	mov dx,#0x3f2
	mov al,#0
	outb
	pop dx
	ret

sectors:
	.word 0

msg1:
	.byte 13,10
	.ascii "Loading system ..."
	.byte 13,10,13,10

.org 508
root_dev:
	.word ROOT_DEV
boot_flag:
	.word 0xAA55

.text
endtext:
.data
enddata:
.bss
endbss:
